<%@page import="it.crosato.stage.shared.objects.MatchElement"%>
<%@page import="it.crosato.stage.shared.objects.Multiset"%>
<%@page import="it.crosato.stage.shared.objects.Match"%>
<%@page import="it.crosato.stage.shared.objects.MultisetsGroup"%>
<%@page import="it.crosato.stage.shared.objects.Reaction"%>
<%@page import="it.crosato.stage.shared.objects.Pathway"%>
<%@page import="it.crosato.stage.shared.objects.EntityDefinition"%>
<%@page import="java.util.Vector"%>
<html>

<head>
	<title>CoMeta - Dettagli vie metaboliche</title>
	<link type="text/css" rel="stylesheet" href="css/web.css" media="screen" />
	<script type="text/javascript">
	window.onload=function(){
		change();
	};
	function change() {
		var path = document.getElementById("path");
		var org1 = document.getElementById("org1");
		var org2 = document.getElementById("org2");
		var id = path.options[path.selectedIndex].value +
			org1.options[org1.selectedIndex].value +
			org2.options[org2.selectedIndex].value;
		var divs = document.getElementsByName("element");
		for(var i=0; i < divs.length; i++) {
			divs[i].style.display="none";
		}
		document.getElementById(id).style.display="block";
	}
	</script>
</head>

<body>

	<div class="app">

	<div class="title">
		<h1>CoMeta</h1>
	</div>
	
	<div class="content">
	
		<div class="side">
			<form action="selection.html" method="get">
				<%long time = Long.class.cast(session.getAttribute("time")); %>
				<input type="hidden" name="time" value="<%out.print(time);%>">
				<input type="image" src="pics/freccia-sx.png">
			</form>
		</div>
		
		<div class=result>
			<div class="navigation">
				<ul>
				    <li><a href="pathways.html">Vie metaboliche</a></li>
				    <li><a href="sets.html">Insiemi</a></li>
				    <li id="activelink"><a href="invariants.html">Reti di Petri</a></li>
				    <li><a href="mapping.html">Matrici distanza</a></li>
				    <li><a href="tree.html">Albero filogenetico</a></li>
				</ul>
			</div>
			<div class="resultBox">
			<%
				// recupero dei parametri
				@SuppressWarnings("unchecked")
				Vector<EntityDefinition> selectedOrganisms = (Vector<EntityDefinition>)
						session.getAttribute("selectedOrganisms");
				@SuppressWarnings("unchecked")
				Vector<EntityDefinition> selectedPathways = (Vector<EntityDefinition>)
						session.getAttribute("selectedPathways");
				MultisetsGroup[][] invariants = MultisetsGroup[][].class.cast(session.getAttribute("invariants"));
				@SuppressWarnings("unchecked")
				Vector<Match[][]> matches = (Vector<Match[][]>)session.getAttribute("matches"); 
				// menu selezione
				out.println("<div class=\"choice\">");
				out.println("<label for=\"path\">Via metabolica</label><br />");
				out.println("<select id=\"path\" onChange=\"javascript:change()\">");
				for (int j = 0; j < selectedPathways.size(); j++) {
					out.println("<option>" + selectedPathways.get(j).getDescription() + "</option>");
				}
				out.println("</select><br />");
				out.println("<label for=\"org1\">Organismo 1</label><br />");
				out.println("<select id=\"org1\" onChange=\"javascript:change()\">");
				for (int i = 0; i < selectedOrganisms.size(); i++) {
					out.println("<option>" + selectedOrganisms.get(i).getDescription() + "</option>");
				}
				out.println("</select><br />");
				out.println("<label for=\"org2\">Organismo 2</label><br />");
				out.println("<select id=\"org2\" onChange=\"javascript:change()\">");
				for (int i = 0; i < selectedOrganisms.size(); i++) {
					out.println("<option>" + selectedOrganisms.get(i).getDescription() + "</option>");
				}
				out.println("</select>");
				out.println("</div>");
				// mostro tutti i confronti per ogni pathway
				for (int k = 0; k < selectedPathways.size(); k++) {
					for (int i = 0; i < selectedOrganisms.size(); i++) {
						for (int j = 0; j < selectedOrganisms.size(); j++) {
							// nome del div
							String id = selectedPathways.get(k).getDescription() +
								selectedOrganisms.get(i).getDescription() +
								selectedOrganisms.get(j).getDescription();
							// mapping degli invarianti
							out.println("<div class=\"matchTable\" name=\"element\" id=\"" + id + "\">");
							MultisetsGroup invGrpX = invariants[i][k];
							MultisetsGroup invGrpY = invariants[j][k];
							out.println("<table class=\"invMatching\">");
							Vector<Integer> mem = new Vector<Integer>();
							out.println("<th>Invarianti organismo 1</th><th>distanza</th><th>Invarianti organismo 1</th>");
							for (int q = 0; q < invGrpX.getNumberOfMultisets(); q++) {
								out.println("<tr>");
								Multiset invX = invGrpX.getMultiset(q);
								MatchElement matchEl = matches.get(k)[i][j].getByX(q);
								if (matchEl != null) {
									mem.add(matchEl.getY());
									Multiset invY = invGrpY.getMultiset(matchEl.getY());
									
									out.print("<td>");
									for (int w = 0; w < invX.getNumberOfElements(); w++) {
										int pos = -1;
										for (int z = 0; z < invY.getNumberOfElements() && pos==-1; z++) {
											if (invX.getMultisetElement(w).getElement().equals
													(invY.getMultisetElement(z).getElement()))
												pos = z;
										}
										String color;
										if (pos == -1)
											color = "red";
										else
											if (invX.getMultisetElement(w).getCardinality() == invY.getMultisetElement(pos).getCardinality())
												color = "green";
											else
												color = "yellow";
										out.println("<a target=\"blank\" href=\"http://www.genome.jp/dbget-bin/www_bget?" + invX.getMultisetElement(w).getElement()
												+ "\"><spam style=\"color:"+color+";\">"+ invX.getMultisetElement(w).getElement() +
												" x " + invX.getMultisetElement(w).getCardinality() + "</spam></a><br />");
									}
									
									out.println("</td><td>" + matchEl.getValue() + "</td><td>");
									
									for (int w = 0; w < invY.getNumberOfElements(); w++) {
										int pos = -1;
										for (int z = 0; z < invX.getNumberOfElements() && pos==-1; z++) {
											if (invY.getMultisetElement(w).getElement().equals
													(invX.getMultisetElement(z).getElement()))
												pos = z;
										}
										String color;
										if (pos == -1)
											color = "red";
										else
											if (invY.getMultisetElement(w).getCardinality() == invX.getMultisetElement(pos).getCardinality())
												color = "green";
											else
												color = "yellow";
										out.println("<a target=\"blank\" href=\"http://www.genome.jp/dbget-bin/www_bget?" + invY.getMultisetElement(w).getElement()
												+ "\"><spam style=\"color:"+color+";\">"+ invY.getMultisetElement(w).getElement() +
												" x " + invY.getMultisetElement(w).getCardinality() + "</spam></a><br />");
									}
									out.println("</td></tr>");
								}
								else {
									out.print("<td>");
									for (int w = 0; w < invX.getNumberOfElements(); w++) {
										out.println("<spam style=\"color:red\">"+invX.getMultisetElement(w).getElement() +
												" x " + invX.getMultisetElement(w).getCardinality() +"</spam><br />");
									}
									out.println("</td><td>/</td><td>/</td></tr>");
								}
							}
							for (int q = 0; q < invGrpY.getNumberOfMultisets(); q++) {
								if (! mem.contains(q)) {
									out.println("<tr><td>/</td><td>/</td><td>");
									Multiset invY = invGrpY.getMultiset(q);
									for (int w = 0; w < invY.getNumberOfElements(); w++) {
										out.println("<a target=\"blank\" href=\"http://www.genome.jp/dbget-bin/www_bget?" + invY.getMultisetElement(w).getElement()
												+ "\"><spam style=\"color:red;\">"+ invY.getMultisetElement(w).getElement() +
												" x " + invY.getMultisetElement(w).getCardinality() + "</spam></a><br />");
									}
									out.println("</td><tr>");
								}
							}
							out.println("</table>");
							out.println("</div>");
						}
					}
				}
			%>
			</div>
		</div>
	</div>
	
	<div class="footer">
		<span>Strumento per la comparazione di reti metaboliche</span>
	</div>
	
	</div>

</body>

</html>